package com.jamlu.framework.base;

import android.support.annotation.NonNull;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jamlu.framework.model.bean.BaseBean;
import com.jamlu.framework.model.bean.BaseListBean;
import com.jamlu.framework.presenter.IBaseRxPresenter;
import com.jamlu.framework.ui.status.IStatus;
import com.jamlu.framework.ui.status.RequestUtils;
import com.jamlu.framework.ui.status.StatusRequest;
import com.jamlu.framework.ui.status.StatusSmartRvView;
import com.jamlu.framework.ui.status.StatusUtils;
import com.jamlu.framework.ui.view.IStatusView;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by DMing at 2018/3/1 0001
 */

public abstract class BaseRxStatusActivity<T extends IBaseRxPresenter>  extends BaseRxActivity<T> implements IStatusView{

    @Override
    public void injectStatusToast(@NonNull StatusRequest statusRequest){
        if (presenter == null)return;
        StatusUtils.initStatusToast(this,presenter.getDisposables(), statusRequest);
    }

    @Override
    public void injectStatusDialog(@NonNull StatusRequest statusRequest){
        if (presenter == null)return;
        StatusUtils.initStatusDialog(this, presenter.getDisposables(), true, statusRequest);
    }

    @Override
    public void injectStatusDialog(boolean cancelable,@NonNull StatusRequest statusRequest){
        if (presenter == null)return;
        StatusUtils.initStatusDialog(this, presenter.getDisposables(), cancelable, statusRequest);
    }

    @Override
    public void injectStatusView(@NonNull ViewGroup viewGroup, @NonNull StatusRequest statusRequest){
        if (presenter == null)return;
        StatusUtils.initStatusOfficialView(viewGroup,presenter.getDisposables(), statusRequest);
    }

    @Override
    public StatusSmartRvView injectStatusSmartRv(@NonNull ViewGroup viewGroup, @NonNull BaseQuickAdapter baseQuickAdapter, @NonNull StatusRequest statusRequest){
        if (presenter == null)return null;
        return StatusUtils.initStatusSmartRvView(viewGroup, baseQuickAdapter, presenter.getDisposables(), statusRequest);
    }

    @Override
    public StatusSmartRvView hasDataSmartRvView(@NonNull ViewGroup viewGroup, @NonNull BaseQuickAdapter baseQuickAdapter, @NonNull List list, @NonNull StatusRequest statusRequest){
        if (presenter == null)return null;
        return StatusUtils.hasDataSmartRvView(viewGroup, baseQuickAdapter,list, presenter.getDisposables(), statusRequest);
    }
/////////////////////////////////////////////////////////////////////////////////////////////
    @Override
    public void requestStatusDefine(@NonNull IStatus iStatus,@NonNull  Observable requestObservable){
        RequestUtils.request(requestObservable)
                .iStatus(iStatus)
                .listener();
    }

    @Override
    public void requestStatusToast(@NonNull final Observable requestObservable){
        injectStatusToast(new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable)
                        .iStatus(iStatus)
                        .listener();
            }
        });
    }

    @Override
    public void requestStatusDialog(@NonNull final Observable requestObservable){
        injectStatusDialog(new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable)
                        .iStatus(iStatus)
                        .listener();
            }
        });
    }

    @Override
    public void requestStatusDialog(boolean cancelable,@NonNull final Observable requestObservable){
        injectStatusDialog(cancelable,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable)
                        .iStatus(iStatus)
                        .listener();
            }
        });
    }

    @Override
    public void requestStatusView(@NonNull ViewGroup viewGroup,@NonNull final Observable requestObservable){
        injectStatusView(viewGroup,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable)
                        .iStatus(iStatus)
                        .listener();
            }
        });
    }

    @Override
    public StatusSmartRvView requestStatusSmartRv(@NonNull ViewGroup viewGroup, @NonNull BaseQuickAdapter baseQuickAdapter,@NonNull final Observable requestObservable){
        return injectStatusSmartRv(viewGroup,baseQuickAdapter,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable)
                        .iStatus(iStatus)
                        .listenerList();
            }
        });
    }

    @Override
    public StatusSmartRvView requestHasDataSmartRv(@NonNull ViewGroup viewGroup, @NonNull  BaseQuickAdapter baseQuickAdapter, @NonNull List list, @NonNull final Observable requestObservable){
        return hasDataSmartRvView(viewGroup,baseQuickAdapter,list,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable)
                        .iStatus(iStatus)
                        .listenerList();
            }
        });
    }
    /////////////////////////////////////////////////////////////////////////////////////////////
    @Override
    public void requestStatusDefine(@NonNull IStatus iStatus, @NonNull Observable requestObservable,@NonNull  final Consumer<BaseBean> consumer){
        RequestUtils.request(requestObservable)
                .iStatus(iStatus)
                .listener(consumer);
    }

    @Override
    public void requestStatusToast(@NonNull final Observable requestObservable, @NonNull final Consumer<BaseBean> consumer){
        injectStatusToast(new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable)
                        .iStatus(iStatus)
                        .listener(consumer);
            }
        });
    }

    @Override
    public void requestStatusDialog(@NonNull final Observable requestObservable,@NonNull  final Consumer<BaseBean> consumer){
        injectStatusDialog(new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable)
                        .iStatus(iStatus)
                        .listener(consumer);
            }
        });
    }

    @Override
    public void requestStatusDialog(boolean cancelable,@NonNull final Observable requestObservable, @NonNull final Consumer<BaseBean> consumer){
        injectStatusDialog(cancelable,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable)
                        .iStatus(iStatus)
                        .listener(consumer);
            }
        });
    }

    @Override
    public void requestStatusView(@NonNull ViewGroup viewGroup,@NonNull final Observable requestObservable, @NonNull final Consumer<BaseBean> consumer){
        injectStatusView(viewGroup,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable)
                        .iStatus(iStatus)
                        .listener(consumer);
            }
        });
    }

    @Override
    public StatusSmartRvView requestStatusSmartRv(@NonNull ViewGroup viewGroup, @NonNull BaseQuickAdapter baseQuickAdapter,@NonNull final Observable requestObservable, @NonNull final Consumer<BaseListBean> consumer){
        return injectStatusSmartRv(viewGroup,baseQuickAdapter,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable)
                        .iStatus(iStatus)
                        .listenerList(consumer);
            }
        });
    }

    @Override
    public StatusSmartRvView requestHasDataSmartRv(@NonNull ViewGroup viewGroup, @NonNull  BaseQuickAdapter baseQuickAdapter, @NonNull List list, @NonNull final Observable requestObservable, @NonNull  final Consumer<BaseListBean> consumer){
        return hasDataSmartRvView(viewGroup,baseQuickAdapter,list,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable)
                        .iStatus(iStatus)
                        .listenerList(consumer);
            }
        });
    }

    /////////////////////////////////////////////////////////////////////////////////////////////
    @Override
    public void requestStatusDefine(@NonNull IStatus iStatus, @NonNull Observable requestObservable,@NonNull  Consumer<BaseBean> consumer,@NonNull Consumer<Integer> failureStatus){
        RequestUtils.request(requestObservable)
                .iStatus(iStatus)
                .listener(consumer,failureStatus);
    }

    @Override
    public void requestStatusToast(@NonNull final Observable requestObservable, @NonNull final Consumer<BaseBean> consumer,@NonNull final Consumer<Integer> failureStatus){
        injectStatusToast(new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable)
                        .iStatus(iStatus)
                        .listener(consumer,failureStatus);
            }
        });
    }

    @Override
    public void requestStatusDialog(@NonNull final Observable requestObservable, @NonNull final Consumer<BaseBean> consumer,@NonNull  final Consumer<Integer> failureStatus){
        injectStatusDialog(new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable)
                        .iStatus(iStatus)
                        .listener(consumer,failureStatus);
            }
        });
    }

    @Override
    public void requestStatusDialog(boolean cancelable,@NonNull final Observable requestObservable, @NonNull final Consumer<BaseBean> consumer,@NonNull final Consumer<Integer> failureStatus){
        injectStatusDialog(cancelable,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable)
                        .iStatus(iStatus)
                        .listener(consumer,failureStatus);
            }
        });
    }

    @Override
    public void requestStatusView(@NonNull ViewGroup viewGroup ,@NonNull final Observable requestObservable, @NonNull final Consumer<BaseBean> consumer,@NonNull final Consumer<Integer> failureStatus){
        injectStatusView(viewGroup,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable)
                        .iStatus(iStatus)
                        .listener(consumer,failureStatus);
            }
        });
    }

    @Override
    public StatusSmartRvView requestStatusSmartRv(@NonNull ViewGroup viewGroup,@NonNull  BaseQuickAdapter baseQuickAdapter, @NonNull final Observable requestObservable,@NonNull  final Consumer<BaseListBean> consumer,@NonNull  final Consumer<Integer> failureStatus){
        return injectStatusSmartRv(viewGroup,baseQuickAdapter,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable)
                        .iStatus(iStatus)
                        .listenerList(consumer,failureStatus);
            }
        });
    }

    @Override
    public StatusSmartRvView requestHasDataSmartRv(@NonNull ViewGroup viewGroup, @NonNull  BaseQuickAdapter baseQuickAdapter, @NonNull List list, @NonNull final Observable requestObservable, @NonNull  final Consumer<BaseListBean> consumer, @NonNull  final Consumer<Integer> failureStatus){
        return hasDataSmartRvView(viewGroup,baseQuickAdapter,list,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable)
                        .iStatus(iStatus)
                        .listenerList(consumer,failureStatus);
            }
        });
    }
}
